package com.zlzlib.liblayoutmanager;

import android.os.Handler;
import android.os.Looper;
import android.util.Log;
import android.view.animation.DecelerateInterpolator;
import android.widget.Scroller;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.RecyclerView;

/**
 * @Desc: 自动滚动的工具类
 * @Copyright:
 * @DateTime: 2021/8/17 18:01
 * @Author zlz
 * @Version 1.0
 */
public class AutoScrollSnapUtils extends CenterSnapUtils {

    final static int BACK = 1;
    final static int FORWARD = 2;
    final static int TIME_INTERVAL = 2000;
    final static int MOVE_STEP = 5;
    final static int NORMAL = 1;
    final static int PAGE = 2;

    private final Handler handler;
    private Runnable autoPlayRunnable;
    private boolean runnableAdded;
    //滚动时间间隔
    private int timeInterval;
    //滚动方向
    private int direction;
    //滚动类型
    private final int scrollType;
    //每次滚动距离
    private int moveStep;


    public AutoScrollSnapUtils(int timeInterval, int direction, int scrollType, int moveStep) {
        checkTimeInterval(timeInterval);
        checkDirection(direction);
        checkScrollType(direction);
        checkMoveStep(direction);
        handler = new Handler(Looper.getMainLooper());
        this.timeInterval = timeInterval;
        this.direction = direction;
        this.scrollType = scrollType;
        this.moveStep = moveStep;
    }

    @Override
    public void attachToRecyclerView(@Nullable RecyclerView recyclerView) throws IllegalStateException {
        if (mRecyclerView == recyclerView) {
            return;
        }
        if (mRecyclerView != null) {
            destroyCallbacks();
        }
        mRecyclerView = recyclerView;
        if (mRecyclerView != null) {
            final RecyclerView.LayoutManager layoutManager = mRecyclerView.getLayoutManager();
            if (!(layoutManager instanceof BaseLayoutManager)) return;
            if (scrollType == PAGE) {
                setupCallbacks();
                mGravityScroller = new Scroller(mRecyclerView.getContext(), new DecelerateInterpolator());
                snapToCenterView((BaseLayoutManager) layoutManager, ((BaseLayoutManager) layoutManager).getPageChangeListener());
            }
            ((BaseLayoutManager) layoutManager).setInfiniteLoop(true);
            autoPlayRunnable = new Runnable() {
                @Override
                public void run() {
                    if (scrollType == PAGE) {
                        final int currentPosition = ((BaseLayoutManager) layoutManager).getCurrentPositionOffset() *
                                (((BaseLayoutManager) layoutManager).getReverseLayout() ? -1 : 1);
                        //移动到下一页
                        ScrollUtils.smoothScrollToPosition(mRecyclerView, (BaseLayoutManager) layoutManager,
                                direction == FORWARD ? currentPosition + 1 : currentPosition - 1);
                    } else {
                        ScrollUtils.smoothScrollToOffset(mRecyclerView, (BaseLayoutManager) layoutManager, direction == FORWARD ? moveStep : -moveStep);
                    }
                    handler.postDelayed(autoPlayRunnable, timeInterval);
                }
            };
            handler.postDelayed(autoPlayRunnable, timeInterval);
            runnableAdded = true;
        }
    }

    @Override
    void destroyCallbacks() {
        super.destroyCallbacks();
        //移除定时任务
        if (runnableAdded) {
            handler.removeCallbacks(autoPlayRunnable);
            runnableAdded = false;
        }
    }

    private void checkDirection(int direction) {
        if (direction != BACK && direction != FORWARD)
            throw new IllegalArgumentException("direction 应该是 BACK 或 FORWARD 之一");
    }

    private void checkScrollType(int scrollType) {
        if (scrollType != NORMAL && scrollType != PAGE)
            throw new IllegalArgumentException("scrollType 应该是 NORMAL 或 PAGE 之一");
    }

    private void checkTimeInterval(int timeInterval) {
        if (timeInterval <= 0)
            throw new IllegalArgumentException("timeInterval 应该大于0");
    }

    private void checkMoveStep(int moveSpeed) {
        if (moveSpeed <= 0)
            throw new IllegalArgumentException("moveSpeed 应该大于0");
    }

    void pause() {
        if (runnableAdded) {
            handler.removeCallbacks(autoPlayRunnable);
            runnableAdded = false;
        }
    }

    void start() {
        if (!runnableAdded) {
            handler.postDelayed(autoPlayRunnable, timeInterval);
            runnableAdded = true;
        }
    }

    void setTimeInterval(int timeInterval) {
        checkTimeInterval(timeInterval);
        this.timeInterval = timeInterval;
    }

    void setDirection(int direction) {
        checkDirection(direction);
        this.direction = direction;
    }

    void setMoveStep(int moveStep) {
        checkMoveStep(moveStep);
        this.moveStep = moveStep;
    }
}
